package com.wt.service.impl;

import com.wt.dao.OrderInforItemMapper;
import com.wt.dao.WithdrawOrderMapper;
import com.wt.pojo.WithdrawOrder;
import com.wt.service.WithDrawService;
import com.wt.util.Pager;
import com.wt.util.Pagination;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Author hiberk
 * Date 2017/6/29 14:40
 */
@Service
public class WithDrawServiceImpl implements WithDrawService {
    @Resource
    private WithdrawOrderMapper withDrawOrderMapper;
    @Resource
    private OrderInforItemMapper orderInforItemMapper;

    @Override
    public boolean selectCheckStatus(Integer id) {
        return withDrawOrderMapper.selectCheckStatus(id) == 0;
    }

    @Override
    public boolean saveWithdrawOrder(WithdrawOrder order) {
        return withDrawOrderMapper.insertSelective(order)>0;
    }

    @Override
    public BigDecimal selectTradeCommission() {
        return orderInforItemMapper.selectTradeCommission();
    }

    @Override
    public Long selectCountOrders() {
        return orderInforItemMapper.selectCountOrders();
    }

    @Override
    public BigDecimal selectTradeAll() {
        return orderInforItemMapper.selectTradeCountByAll();
    }

    @Override
    public BigDecimal selectAccountAll() {
        return orderInforItemMapper.selectAccountAll();
    }

    @Override
    public BigDecimal selectAccountTotal(Integer id) {
        return orderInforItemMapper.selectAccountTotal(id);
    }

    @Override
    public BigDecimal selectTradeTotal(Integer id) {
        return orderInforItemMapper.selectTradeCountByDistributorId(id);
    }

    @Override
    public Pagination<WithdrawOrder> selectWithDrawOrders(Integer id, Long timeStart, Long timeEnd, Pager pager) {
        List<WithdrawOrder> withDrawOrders = withDrawOrderMapper.selectWithDrawOrders(id, timeStart, timeEnd, pager.getStartIndex(), pager.getPageSize());
        for (WithdrawOrder withdrawOrder:withDrawOrders) {
            if (withdrawOrder.getSendTime() != null) {
                Date sendTime = new Date(withdrawOrder.getSendTime()*1000);
                withdrawOrder.setSentd(sendTime);
            }
            if (withdrawOrder.getCheckTime() != null) {
                Date checkTime = new Date(withdrawOrder.getCheckTime()*1000);
                withdrawOrder.setCheck(checkTime);
            }
        }
        Pagination<WithdrawOrder> pagination = new Pagination<WithdrawOrder>();
        Long count = (long)withDrawOrderMapper.selectCountOrders(id, timeStart, timeEnd).size();
        pager.calcPageCount(count);
        pager.setRowCount(count);
        pagination.setRecords(withDrawOrders);
        pagination.setPager(pager);
        return pagination;
    }
}
